package com.junit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hcfly.utils.ESClient;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;

public class SearchIdDemo {

    private static final ObjectMapper mapper = new ObjectMapper();
    private static final String index = "fruit";
    private static final String type = "species";
    private static final RestHighLevelClient client = ESClient.getClient();

    @Test
    public void getByRange() throws IOException {
        /**
         * 范围查询，只针对数值
         */
        SearchRequest request = new SearchRequest(index);
        request.types(type);

        //指定查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
//        gte:>= gt>, lte <= lt <
        builder.query(QueryBuilders.rangeQuery("size").gte(4).lte(9));

        request.source(builder);

        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : resp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }


    @Test
    public void getByWildCard() throws IOException {
        /**
         * 模糊查询，允许出现错别字
         */
        SearchRequest request = new SearchRequest(index);
        request.types(type);

        //指定查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
//        prefixLength为前面必须多少个字要正确
        builder.query(QueryBuilders.wildcardQuery("name", "?果"));

        request.source(builder);

        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : resp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }


    @Test
    public void getByFuzzy() throws IOException {
        /**
         * 模糊查询，允许出现错别字
         */
        SearchRequest request = new SearchRequest(index);
        request.types(type);

        //指定查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
//        prefixLength为前面必须多少个字要正确
        builder.query(QueryBuilders.fuzzyQuery("name", "奇艺果").prefixLength(1));

        request.source(builder);

        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : resp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }


    @Test
    public void getByPrefix() throws IOException {
        SearchRequest request = new SearchRequest(index);
        request.types(type);

        //指定查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.prefixQuery("name", "菠"));

        request.source(builder);

        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : resp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }

    @Test
    public void getByIds() throws IOException {
        SearchRequest request = new SearchRequest(index);
        request.types(type);

        //指定查询条件

        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.idsQuery().addIds("1","2","3"));

        request.source(builder);

        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : resp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }



    }

    @Test
    public void getById() throws IOException {
        /**
         * 通过id来查询
         */
        GetRequest request = new GetRequest(index,type, "1");
        GetResponse resp = client.get(request, RequestOptions.DEFAULT);
        System.out.println(resp.getSourceAsMap());
    }

}
